package com.ubao.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;

import com.ubao.util.log.Logger;


public class Drawer {

	/* 加入判断未开盘情况 */
	public static String format(int v, int current, int d) {
		if (current == 0)
			return "—";

		return format(v, d);
	}

	/* 若小数位大于2，只保留二位小数 */
	public static String formatWithTwoDecimal(int v, int d) {
		String s = format(v, d);
		if (d <= 2)
			return s;

		return "" + new BigDecimal(s).setScale(2, BigDecimal.ROUND_HALF_UP);
	}

	/* 若小数位大于2，只保留二位小数 加入判断未开盘情况 */
	public static String formatWithTwoDecimal(int v, int current, int d) {
		if (current == 0)
			return "—";

		return formatWithTwoDecimal(v, d);
	}

	public static String format(int v, int d) {
		// System.out.println("weibi is :"+v);
		String s = String.valueOf(Math.abs(v));
		while (s.length() <= d)
			s = "0" + s;
		if (v < 0)
			s = "-" + s;
		return s.substring(0, s.length() - d) + "."
				+ s.substring(s.length() - d);
	}

	public static int getColor(int v) {
		return v == 0 ? 0xFFFFFFFF : (v > 0 ? 0xFFFF0000 : 0xFF00CC00);
	}

	public static int getColor(int r, int c) {
		return getColor((r == 0 || c == 0) ? 0 : r - c);
	}

	public static int getRate(int r, int c) {
		if (r == 0 || c == 0)
			return 0;
		long v = (long) (r - c) * 1000000 / c;
		return (int) ((v + (v >= 0 ? 50 : -50)) / 100);
	}

	public static String formatPrice(int v, int d) {
		return v == 0 ? "—" : format(v, d);
	}

	public static String formatVolumn(long v) {
		String s = String.valueOf(v);

		if (v <= 0)
			return "—";
		else if (s.length() > 6)
			return (v / 10000) + "万";
		else if (s.length() == 6) {
			DecimalFormat df = new DecimalFormat("#0.0");
			return df.format(v / 10000.0) + "万";
		}

		return s;
	}

	public static String formatDelta(int r, int c, int d) {
		return (r == 0 || c == 0) ? "—" : "" + format(r - c, d);
		// return (r == 0 || c == 0) ? "-" : (r > c ? "+" : "") + format(r - c,
		// d);
	}

	public static String formatRate(int r, int c) {
		return (r == 0 || c == 0) ? "—" : "" + format(getRate(r, c), 2) + "%";
	}

	public static String formatRateWithoutSymbol(int r, int c) {
		return (r == 0 || c == 0) ? "—" : "" + format(getRate(r, c), 2);
	}

	public static String formatTime(int t) {
		try {
			String tStr = String.valueOf(t);
			int strLength = tStr.length();
			if (strLength == 5) {
				tStr = "0" + tStr;
			} else if (strLength == 4) {
				tStr = "00" + tStr;
			} else if (strLength == 3) {
				tStr = "000" + tStr;
			} else if (strLength == 2) {
				tStr = "0000" + tStr;
			} else if (strLength == 1) {
				tStr = "00000" + tStr;
			}
			return tStr.substring(0, 2) + ":" + tStr.substring(2, 4);
		} catch (Exception e) {
			return "-";
		}
	}

	public static String formatDate(int v) {
		String temp = Integer.toString(v);

		String returnValue = temp.substring(0, 4);
		returnValue += "-";
		returnValue += temp.substring(4, 6);
		returnValue += "-";
		returnValue += temp.substring(6);

		return returnValue;

	}

	public static int iParseRound(int iNum) {// 四舍五入
		int iRound = 0;
		String strNum = String.valueOf(iNum);

		int iNumLen = strNum.length();
		int strLastNum = 0;

		if (iNumLen > 1) {
			strLastNum = Integer.parseInt(strNum.substring(iNumLen - 1));

			if (strLastNum > 4) {
				iRound = Integer.parseInt(strNum.substring(0, iNumLen - 1)) + 1;
			} else {
				iRound = Integer.parseInt(strNum.substring(0, iNumLen - 1));
			}
		}
		return iRound;
	}

	public static String parseIntToTimeWithYear(int v) {// added by wyp
		/*
		 * struct MinKLTime { unsigned m_nMin:6; unsigned m_nHour:5; unsigned
		 * m_nDay:5; unsigned m_nMonth:4; unsigned m_nYear:12; };
		 */

		String strTmp = "";
		StringBuffer sbTmp = new StringBuffer();
		int hi = v >> 16;
		int lo = v & 0xffff;
		int year = (hi >> 4) & 0x0fff;
		int month = hi & 0x0f;
		int day = (lo >> 11) & 0x1f;
		int hour = (lo >> 6) & 0x1f;
		int minute = lo & 0x3f;

		sbTmp.append(year + "");

		if (String.valueOf(month).length() < 2) {
			sbTmp.append("0" + month);
		} else {
			sbTmp.append(month + "");
		}

		if (String.valueOf(day).length() < 2) {
			sbTmp.append("0" + day);
		} else {
			sbTmp.append("" + day);
		}

		if (String.valueOf(hour).length() < 2) {
			sbTmp.append("0" + hour);
		} else {
			sbTmp.append("" + hour);
		}

		if (String.valueOf(minute).length() < 2) {
			sbTmp.append("0" + minute);
		} else {
			sbTmp.append("" + minute);
		}

		strTmp = sbTmp.toString();
		return strTmp;
	}

	public static String parseIntToTime(int v) {
		String strTmp = "";
		StringBuffer sbTmp = new StringBuffer();
		int hi = v >> 12;
		int lo = v & 0xffff;
		int month = (hi >> 4) & 0x0f;
		int day = (lo >> 11) & 0x1f;
		int hour = (lo >> 6) & 0x1f;
		int minute = lo & 0x3f;

		if (String.valueOf(month).length() < 2) {
			sbTmp.append("0" + month);
		} else {
			sbTmp.append(month + "");
		}

		if (String.valueOf(day).length() < 2) {
			sbTmp.append("0" + day);
		} else {
			sbTmp.append("" + day);
		}

		if (String.valueOf(hour).length() < 2) {
			sbTmp.append("0" + hour);
		} else {
			sbTmp.append("" + hour);
		}

		if (String.valueOf(minute).length() < 2) {
			sbTmp.append("0" + minute);
		} else {
			sbTmp.append("" + minute);
		}

		strTmp = sbTmp.toString();
		return strTmp;
	}

	public static long parseLong(int v) {
		int v1 = (v >>> 30) & 0x03;
		// System.out.println("v1 is :"+v1);
		if (v1 == 0)
			return v;
		else {
			long v2 = v & 0x3FFFFFFF;
			return v2 << (v1 * 4);
		}
	}

	// 交易量错误处理
	public static int parseInt(int v) {
		int v1 = (v >>> 30) & 0x03;
		// System.out.println("v1 is :"+v1);
		if (v1 == 0)
			return v;
		else {
			int v2 = v & 0x3FFFFFFF;
			return v2;
		}
	}

	public static long parseLargeInt(int intV) {
		final long v = Drawer.parseUInt(intV);
		final byte head = (byte) ((v >>> 30) & 0x03);
		if (head != 0) {
			return (v & 0x3fffffff) << (head * 4);
		}

		return v;
	}

	/**
	 * 
	 * @param v
	 * @return 无符号整型
	 */
	public static long parseUInt(int v) {
		long byte0 = v & 0x000000ff;
		long byte1 = (v >>> 8) & 0x000000ff;
		long byte2 = (v >>> 16) & 0x000000ff;
		long byte3 = (v >>> 24) & 0x000000ff;

		return (byte3 << 24) | (byte2 << 16) | (byte1 << 8) | byte0;
	}

	public static double intToDouble(int v, int decLen) {
		double divideLength = 1;
		for (int k = 0; k < decLen; k++) {
			divideLength *= 10;
		}

		return v / divideLength;
	}

	private static String padZero(String v, int count) {
		String ret = "" + v;
		for (int i = 0; i < count; i++) {
			ret += "0";
		}
		return ret;
	}

	public static String formatDecimal(String v, int decLen) {// 8=>8.00,8=>8.000,8.1=>8.10,8.111=>8.11
		if (v.matches("^[0-9]+$")) {// 8=>8.00,8=>8.000
			if (decLen < 1) {
				return v;
			}

			return padZero(v + ".", decLen);
		}

		if (v.matches("^[0-9]+.[0-9]+$")) {// 8.1=>8.10,8.111=>8.11
			final String[] splits = v.split("\\.");
			final String intPart = splits[0];// 整数部分
			final String decPart = splits[1];// 小数部分

			if (decLen < 1) {
				return intPart;
			}

			final int decimalDelta = decLen - decPart.length();
			if (decimalDelta >= 0) {
				return padZero(v, decimalDelta);
			} else {
				return v.substring(0, v.length() + decimalDelta);
			}
		}

		return v;
	}

	// 总手：当总手>100万时，显示整数位+万，例如：157万。
	// 当总手>10万时，保留一位小数，例如14.1万。
	// 当总手<10万时，显示整数位，例如56549
	public static String formatTotalHand(final int totalHand,
			final int openPrice) {
		String sTotalHand = String.valueOf((int) (Drawer.parseLong(totalHand)));
		String rTotalHand = "";

		if (totalHand <= 0)
			rTotalHand = openPrice == 0 ? "—" : "0";
		else if (sTotalHand.length() > 6)
			rTotalHand = (totalHand / 10000) + "万";
		else if (sTotalHand.length() == 6) {
			DecimalFormat df = new DecimalFormat("#0.0");
			rTotalHand = df.format(totalHand / 10000.0) + "万";
		} else {
			rTotalHand = sTotalHand;
		}

		return rTotalHand;
	}

	// 总额：当总额>千亿时，保留整数+亿。
	// 当总额>亿元，<千亿时，保留2位小数+亿，例如：100.63亿，21.23亿，2.36亿。
	// 当总额<亿元时，保留1位小数+万，例如：2232.2万
	public static String formatTotalCount(final int totalCount,
			final int openPrice) {

		String sTotalCount = String
				.valueOf((Drawer.parseLong(totalCount)) * 10000);
		String rTotalCount = "";

		if (totalCount <= 0) {
			rTotalCount = openPrice == 0 ? "—" : "0";
		} else if (sTotalCount.length() < 5) {
			rTotalCount = sTotalCount;
		} else if (sTotalCount.length() < 9 && sTotalCount.length() >= 5) {
			DecimalFormat df = new DecimalFormat("#0.0");
			rTotalCount = df.format(totalCount / 1.0) + "万";
		} else if (sTotalCount.length() >= 9 && sTotalCount.length() < 12) {
			DecimalFormat df = new DecimalFormat("#0.00");
			rTotalCount = df.format(totalCount / 10000.0) + "亿";
		} else if (sTotalCount.length() >= 12) {
			rTotalCount = totalCount / 10000 + "亿";
		}

		return rTotalCount;

	}

	// 当内外盘>10万，保留2位小数+万，例如13.26万。
	// 当内外盘<10万，显示整数位，例如26545
	public static String formatInVol(final int iInVol) {

		String sInVol = Integer.toString(iInVol);
		String rinVol = "";

		if (sInVol.length() >= 6) {
			int num = Integer.parseInt(sInVol.substring(sInVol.length() - 2,
					sInVol.length() - 1));
			if (num >= 5) {
				sInVol = Integer.toString(iInVol + 100);
			}
			rinVol = sInVol.substring(0, sInVol.length() - 4)
					+ "."
					+ sInVol.substring(sInVol.length() - 4, sInVol.length() - 2)
					+ "万";
		}

		return rinVol;
	}

	/**
	 * 四舍五入
	 * 
	 * @param number
	 * @param decimalNum
	 *            保留小数位数
	 * @return
	 */
	public static String formatNumber(double number, int decimalNum) {
		return new BigDecimal(number).setScale(decimalNum,
				BigDecimal.ROUND_HALF_UP).toString();
	}

	public static SpannableString formatStockName(final String name,
			final boolean hasAnnouncement) {
		final String s = name + (hasAnnouncement ? "*" : "");
		Logger.v("MainActivity", "s:" + s);
		final SpannableString sp = new SpannableString(s);
		if (hasAnnouncement) {
			sp.setSpan(new ForegroundColorSpan(Color.YELLOW), name.length(),
					name.length() + 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		}

		return sp;
	}
}
